<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 16:30
 */

namespace Services;


use Helpers\Main;
use Helpers\Tool;
use Helpers\XhException;
use Helpers\HttpClient;

class Feedback
{
    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    /**
     * 获取
     *
     * @throws XhException
     *
     * create by dale  20180822
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('feedback');
            self::$url  = Tool::getValue($config, 'url');
            if (!self::$url) {
                throw new XhException('反馈url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取产品
     *
     * @param int $page
     * @param int $size
     * @param array $param
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180822
     */
    public static function getFeedbackList($page = 0, $size = 20 , $param = [])
    {
        self::init();
        $queParam =  array_merge($param, [
            'page' => $page -1,
            'pageSize' => $size,
            'operatorId' => intval(Main::getLoginUserInfo('userId'))
        ]);
        $response = self::$httpClient->postJson('/api/v1/manage/feedback/list', $queParam);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取获取反馈列表接口失败')  , $response);
        }
//        var_dump($response);die;
        $body = $response->toArray();
        if (!$body || $body['totalCount'] === null || $body['data'] === null) {
//            throw new XhException('无返回数据', $response);
            return ['total' => 0, 'rows' => []];
        }
        $total = $body['totalCount'];
        $data  = $body['data'];
        return ['total' => $total, 'rows' => $data,'footer'=>[0 => ['describe' => '本月处理了'.$body['handleContentNum'].'条内容，总共回复次数'.$body['replyNum']]]];
    }

    /**
     * 回复反馈
     *
     * @param $feedbackId
     * @param $replyContent
     * @throws XhException
     *
     * create by dale  20180823
     */
    public static function replyFeedback($feedbackId, $replyContent)
    {
        self::init();
        if (!$feedbackId || !$replyContent) {
            throw new XhException('缺少参数');
        }
        if (!is_array($feedbackId)) {
            throw new XhException('feedbackIds不为数组');
        }
        $response = self::$httpClient->postJson('/api/v1/manage/feedback/reply', [
            'feedbackIds'   => $feedbackId,
            'replyContent' => $replyContent,
            'replyUserId'  => Main::getLoginUserInfo('userId'),
            'replyUserName'=> Main::getLoginUserInfo('userName')
        ]);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(), 'msg', '调取回复反馈接口失败'), $response);
        }
    }

    /**
     * 删除反馈
     *
     * @param array $feedbackIdList
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function deleteFeedback($feedbackIdList=[]){
        self::init();
        if (!$feedbackIdList) {
            throw new XhException('缺少参数');
        }
        $response = self::$httpClient->postJson('/api/v1/manage/feedback/delete', [
            'feedbackIdList'   => $feedbackIdList,
            'operatorId'  => Main::getLoginUserInfo('userId'),
            'operatorName'=> Main::getLoginUserInfo('userName')
        ]);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取删除反馈接口失败'), $response);
        }
    }

    /**
     * 反馈详情
     *
     * @param $feedbackId
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20180827
     */
    public static function getFeedbackDetail($feedbackId){
        self::init();
        if (!$feedbackId) {
            throw new XhException('缺少参数');
        }
        $response = self::$httpClient->get(sprintf('/api/v1/manage/%s/feedback', $feedbackId));
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取获取反馈详情接口失败'), $response);
        }
        return $response->toArray();
    }

    /**
     * 屏蔽反馈
     *
     * @param $feedbackId
     * @param int $bannedDays
     * @param string $bannedReason
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function shieldUser($feedbackId,$bannedDays=1,$bannedReason=''){
        self::init();
        if (!$feedbackId || !$bannedDays) {
            throw new XhException('缺少参数');
        }
        $response = self::$httpClient->postJson('/api/v1/manage/feedback/shield', [
            'feedbackId'   => $feedbackId,
            'bannedDays'   => $bannedDays,
            'bannedReason'   => $bannedReason,
            'operatorId'  => Main::getLoginUserInfo('userId'),
            'operatorName'=> Main::getLoginUserInfo('userName')
        ]);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取屏蔽反馈接口失败'), $response);
        }
    }

    /**
     * 解除屏蔽接口
     *
     * @param $userId
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function releaseShieldUser($userId){
        self::init();
        if (!$userId) {
            throw new XhException('缺少参数');
        }
        $response = self::$httpClient->patch(sprintf('/api/v1/manage/Release/%s/shield', $userId));
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取解除屏蔽接口失败'), $response);
        }
    }

    /**
     * 获取屏蔽列表
     *
     * @param int $page
     * @param int $pageSize
     * @param int $schoolId
     * @param string $userName
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function getShieldList($page=1, $pageSize=20, $schoolId=0, $userName=''){
        self::init();
        $params = [
            'page' => $page-1,
            'pageSize' => $pageSize
        ];
        if ($schoolId) {
            $params['schoolId'] = $schoolId;
        }
        if ($userName) {
            $params['userName'] = $userName;
        }
        $response = self::$httpClient->get('/api/v1/manage/feedback/banned/list', $params);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取获取屏蔽列表接口失败'), $response);
        }
        $body = $response->toArray();
        if (!$body || $body['totalCount'] === null || $body['data'] === null){
//            throw new XhException('无返回数据', $response);
            return ['total' => 0, 'rows' => []];
        }
        $total = $body['totalCount'];
        $data  = $body['data'];
        return ['total' => $total, 'rows' => $data];
    }


    /**
     * 获取模板列表
     *
     * @return mixed
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function getTemplateList(){
        self::init();
        $params = [
            'userId' => Main::getLoginUserInfo('userId')
        ];
        $response = self::$httpClient->get('/api/v1/manage/feedback/template/list', $params);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取获取模板列表接口失败'), $response);
        }
        return $response->toArray();
    }

    /**
     * 保存模板
     *
     * @param string $content
     * @param null $id
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function saveTemplate($content='',$id=null){
        self::init();
        if (!$content) {
            throw new XhException('缺少参数');
        }
        $params = [
            'content'  => $content,
            'userId'   => Main::getLoginUserInfo('userId'),
            'userName' => Main::getLoginUserInfo('userName')
        ];
        if ($id) {
            $params['id'] = $id;
        }
        $response = self::$httpClient->postJson('/api/v1/manage/feedback/template', $params);
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(), 'msg', '调取保存模板接口失败'), $response);
        }
    }

    /**
     * 删除模板
     *
     * @param $id
     * @throws XhException
     *
     * create by dale  20180824
     */
    public static function delTemplate($id)
    {
        self::init();
        if (!$id) {
            throw new XhException('缺少参数');
        }
        $response = self::$httpClient->delete(sprintf('/api/v1/manage/feedback/%s/template', $id));
        if (!$response->isSuccess()) {
            throw new XhException(Tool::getValue($response->toArray(),'msg','调取删除模板接口失败'), $response);
        }

    }



}